#!/usr/bin/python2
# -*- coding: utf8 -*-
from PyQt4 import QtCore, QtGui, Qt, QtDeclarative
from data.LocalStorage import LocalStorage
from models.Job import Job
from utils.log import log
from utils.exceptions.DataBaseException import DataBaseException

class JobsModel(QtCore.QAbstractListModel):
    START_ROLE = 0
    LONG_ROLE = 1
    NAME_ROLE = 2

    minTimeChange = QtCore.pyqtSignal()
    maxTimeChange = QtCore.pyqtSignal()
    countChange = QtCore.pyqtSignal()
    _fromTime = 0
    _toTime = 0

    def __init__(self, db, fromTime, toTime, limit=9999, parent=None):
        QtCore.QAbstractListModel.__init__(self, parent)
        self.setRoleNames({self.START_ROLE: 'startTime', self.LONG_ROLE: 'longTime', self.NAME_ROLE: 'name'})
        self.jobs = db.getJobs(fromTime, toTime, limit)
        self._fromTime = fromTime
        self._toTime = toTime
        db.addListener(self)
        self.db = db

    def data(self, index, role):
        if not index.isValid: return QtCore.QVariant()
        if role == self.START_ROLE:
            return self.jobs[index.row()].startTime
        if role == self.LONG_ROLE:
            return self.jobs[index.row()].longTime
        if role == self.NAME_ROLE:
            return self.jobs[index.row()].name
        return QtCore.QVariant()

    def rowCount(self, parent=QtCore.QModelIndex()):
        return len(self.jobs)

    def index(self, row, column, ind):
        return self.createIndex(row, column)

    def getCount(self):
        return len(self.jobs)

    def getMaxTimeInSeconds(self):
        result = 0
        for job in self.jobs:
            endTime = job.startTime + job.longTime
            if endTime > result:
                result = endTime
        return result

    def getMinTimeInSeconds(self):
        result = 9999999999
        for job in self.jobs:
            startTime = job.startTime
            if startTime < result:
                result = startTime
        return result

    @QtCore.pyqtSlot(QtCore.QString, QtCore.QString, int, int)
    def addJob(self, name, description, fromTime, longTime):
        try:
            self.db.addJob(Job(12, name, description, fromTime, longTime))
        except DataBaseException, dbException:
            log.error(dbException.getMessage() )

    def onJobAdded(self, job):
        if job.startTime < self._toTime and job.startTime >= self._fromTime:
            self.beginInsertRows(QtCore.QModelIndex(), len(self.jobs), len(self.jobs))
            self.jobs.append(job)
            self.endInsertRows()
            if job.startTime < self.minTimeInSeconds:
                self.minTimeChange.emit()
            if job.startTime > self.maxTimeInSeconds:
                self.maxTimeChange.emit()
            self.countChange.emit()

    count = QtCore.pyqtProperty("int", fget=getCount, notify=countChange)
    minTimeInSeconds = QtCore.pyqtProperty("int", fget=getMinTimeInSeconds, notify=minTimeChange)
    maxTimeInSeconds = QtCore.pyqtProperty("int", fget=getMaxTimeInSeconds, notify=maxTimeChange)

